﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AdventureWorks.Entities;
using AdventureWorksData.DAO;
using AdventureWorks.Helpers;
using AdventureWorks.Business;
using AdventureWorks.View.Infra;

namespace AdventureWorks.View.Controllers
{ 
    public class ProductController : Controller
    {
		private readonly ProductBusiness _productBuzz;
		private readonly ProductModelBusiness _modelBuzz;
		private readonly ProductCategoryBusiness _categoryBuzz;
    	public IEnumerable<Product> Products;

		public ProductController()
		{
			_productBuzz = new ProductBusiness();
			_modelBuzz = new ProductModelBusiness();
			_categoryBuzz = new ProductCategoryBusiness();
		}

		public ProductController(IUnitOfWork entityDb)
		{
			_productBuzz = new ProductBusiness(entityDb);
			_modelBuzz = new ProductModelBusiness(entityDb);
			_categoryBuzz = new ProductCategoryBusiness(entityDb);
		}

		public PartialViewResult Search(String sortOrder, String currentFilter, String searchString, int? page, int? ddlProductCategoryID, int? ddlProductModelID)
		{

			var productFilter = ProductFilter.CreateProductFilter(sortOrder, searchString, ddlProductCategoryID, ddlProductModelID);
			Products = _productBuzz.GetList(productFilter);
			DefineViewBagVariables(productFilter);

			if (Request != null)
			{
				if (Request.HttpMethod == "GET")
					searchString = currentFilter;
				else
					page = 0;
			}
			ViewBag.CurrentFilter = searchString;

			ViewBag.PageSize = int.Parse(SystemResources.PageSize);
			ViewBag.PageIndex = page ?? 0;

			PopulateCategoryDropDown(productFilter.ProductCategoryID);
			PopulateModelDropDown(productFilter.ProductModelID);

			return PartialView("_ProductList", Products);
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			//var result = Index(sortOrder, currentFilter, searchString, page, ddlProductCategoryID, ddlProductModelID);
			//var products = result.ViewData.Model;

			//return PartialView("_ProductList", products);
		}

		public ViewResult Index(String sortOrder, String currentFilter, String searchString, int? page, int? ddlProductCategoryID, int? ddlProductModelID)
        {
			var result = Search(sortOrder, currentFilter, searchString, page, ddlProductCategoryID, ddlProductModelID);
			return View(Products);








			//var productFilter = ProductFilter.CreateProductFilter(sortOrder, searchString, ddlProductCategoryID, ddlProductModelID);
			//Products = _productBuzz.GetList(productFilter);
			//DefineViewBagVariables(productFilter);

			//if (Request != null)
			//{
			//    if (Request.HttpMethod == "GET")
			//        searchString = currentFilter;
			//    else
			//        page = 0;
			//}
			//ViewBag.CurrentFilter = searchString;

			//ViewBag.PageSize = int.Parse(SystemResources.PageSize);
			//ViewBag.PageIndex = page ?? 0;

			//PopulateCategoryDropDown(productFilter.ProductCategoryID);
			//PopulateModelDropDown(productFilter.ProductModelID);

			//return View(Products);
        }

		public void DefineViewBagVariables(ProductFilter productFilter)
    	{
			ViewBag.NameSortParam = String.IsNullOrEmpty(productFilter.SortOrder) ? "Name Desc" : "";
			ViewBag.CostSortParam = productFilter.SortOrder == "Cost" ? "Cost Desc" : "Cost";
			ViewBag.PriceSortParam = productFilter.SortOrder == "Price" ? "Price Desc" : "Price";
			ViewBag.WeightSortParam = productFilter.SortOrder == "Weight" ? "Weight Desc" : "Weight";
			ViewBag.ModifiedSortParam = productFilter.SortOrder == "Modified" ? "Modified Desc" : "Modified";
			ViewBag.SearchString = productFilter.SearchString;
			ViewBag.CurrentSort = productFilter.SortOrder;

			ViewBag.ProductCategoryID = productFilter.ProductCategoryID;
			ViewBag.ProductModelID = productFilter.ProductModelID;
    	}

		public ViewResult Details(int id, int? ddlProductCategoryID, int? ddlProductModelID)
        {
			var productFilter = ProductFilter.CreateProductFilter(String.Empty, String.Empty, ddlProductCategoryID, ddlProductModelID);
			DefineViewBagVariables(productFilter);

			Product product = _productBuzz.Get(id);
            return View(product);
        }

		public ActionResult Edit(int id, int? ddlProductCategoryID, int? ddlProductModelID)
		{
			var productFilter = ProductFilter.CreateProductFilter(String.Empty, String.Empty, ddlProductCategoryID, ddlProductModelID);
			DefineViewBagVariables(productFilter);

			Product product = _productBuzz.Get(id);
			PopulateCategoryDropDown(product.ProductCategory.ID);
			PopulateModelDropDown(product.ProductModel.ID);
			return View(product);
		}

		public ActionResult Create(int? ddlProductCategoryID, int? ddlProductModelID)
        {
			var productFilter = ProductFilter.CreateProductFilter(String.Empty, String.Empty, ddlProductCategoryID, ddlProductModelID);
			DefineViewBagVariables(productFilter);
			PopulateCategoryDropDown(ddlProductCategoryID);
			PopulateModelDropDown(ddlProductModelID);

            return View();
        }

		public void PopulateModelDropDown(object selectedModel = null)
		{
			var productModels = _modelBuzz.GetList("Name", String.Empty);
			ViewBag.ddlProductModelID = new SelectList(productModels, "ID", "Name", selectedModel);
		}

		public void PopulateCategoryDropDown(object selectedModel = null)
		{
			var productCategories = _categoryBuzz.GetList("Name", String.Empty).Where(c => c.productCategory != null);
			ViewBag.ddlProductCategoryID = new SelectList(productCategories, "ID", "Name", selectedModel);
		}

        [HttpPost]
		public ActionResult Create(Product product, int ddlProductCategoryID, int ddlProductModelID, HttpPostedFileBase thumbNailPhotoImage)
        {
            if (ModelState.IsValid)
            {
				product = _productBuzz.Format(product, ddlProductCategoryID, ddlProductModelID, thumbNailPhotoImage, null);
				_productBuzz.Insert(product);
				_productBuzz.Save();

				return RedirectToAction("Index", new
				{
					ddlProductCategoryID = ddlProductCategoryID,
					ddlProductModelID = ddlProductModelID
				});

            }
            return View(product);
        }

        [HttpPost]
		public ActionResult Edit(Product product, int ddlProductCategoryID, int ddlProductModelID, Byte[] thumbNailPhotoOld, HttpPostedFileBase thumbNailPhotoImage)
        {
            if (ModelState.IsValid)
            {
				product = _productBuzz.Format(product, ddlProductCategoryID, ddlProductModelID, thumbNailPhotoImage, thumbNailPhotoOld);
				product.ModifiedDate = DateTime.Now;
				_productBuzz.Update(product);
				_productBuzz.Save();

				return RedirectToAction("Details", new
				{
					id = product.ID,
					ddlProductCategoryID = ddlProductCategoryID,
					ddlProductModelID = ddlProductModelID
				});
            }
            return View(product);
        }

		public ActionResult Delete(int id, int? ddlProductCategoryID, int? ddlProductModelID)
        {
			var productFilter = ProductFilter.CreateProductFilter(String.Empty, String.Empty, ddlProductCategoryID, ddlProductModelID);
			DefineViewBagVariables(productFilter);

			Product product = _productBuzz.Get(id);
            return View(product);
        }

        [HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(int id, int? ddlProductCategoryID, int? ddlProductModelID)
        {
			_productBuzz.Delete(id);
			_productBuzz.Save();

			return RedirectToAction("Index", new
			{
				ddlProductCategoryID = ddlProductCategoryID,
				ddlProductModelID = ddlProductModelID
			});
        }

		public ActionResult Image(int id)
		{
			Product product = _productBuzz.Get(id);
			return new ImageResult(product.ThumbNailPhoto);
		}

        protected override void Dispose(bool disposing)
        {
            _productBuzz.Dispose();
            base.Dispose(disposing);
        }
    }
}